import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('@/views/index.vue')
  },
  {
    path: '/userinfo',
    name: 'userinfo',
    component: () => import('@/views/userinfo.vue')
  },
  {
    path: '/record',
    name: 'record',
    component: () => import('@/views/record.vue')
  },
  {
    path: '/personal',
    name: 'personal',
    component: () => import('@/views/personal.vue')
  },
  {
    path: '/auto',
    name: 'auto',
    component: () => import('@/views/auto.vue')
  },
  {
    path: '*',
    redirect: '/',
  }
]

const router = new VueRouter({
  mode: 'hash',
  routes
})
router.beforeEach((to, from, next) => {
  if (process.env.NODE_ENV === 'development') {
    next()
  } else {
    const token = localStorage.getItem('token')
    if (to.path == '/') {
      if (JSON.stringify(to.query) !== '{}') {
        localStorage.setItem('xqid', to.query.xqid)
      }
    }
    if (!token) {
      if (to.path === '/auto') {
        next()
      } else {
        if (JSON.stringify(to.query) !== '{}') {
          localStorage.setItem('now_url', to.fullPath) //当前页url与参数放入缓存
        }
        next('/auto')
      }
    } else {
      next()
    }
  }

})

export default router
